﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Regions;
using AvalonDock;
using System.Collections.Specialized;
using System.Windows;
using Microsoft.Practices.ServiceLocation;
using System.Diagnostics;

namespace Renci.iRods.Studio.Adaptors
{
    public class PaneRegionAdapter : RegionAdapterBase<Pane>
    {
        private IRegion _region;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="behaviorFactory">Allows the registration of the default set of RegionBehaviors.</param>
        public PaneRegionAdapter(IRegionBehaviorFactory behaviorFactory)
            : base(behaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, Pane regionTarget)
        {
            this._region = region;

            Debug.WriteLine("Adapting " + region.Name);

            region.Views.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        foreach (DockableContent element in e.NewItems)
                        {
                            if (element.State == DockableContentState.None)
                            {
                                regionTarget.Items.Add(element);
                            }
                            else
                            {
                                //  TODO: Call method to activate View/Model if needed
                                element.Show();
                            }

                            RoutedEventHandler handler = null;
                            handler = delegate(object sender1, RoutedEventArgs e1)
                            {
                                if (element.State == DockableContentState.Hidden)
                                {
                                    //  TODO: Call method to deactivate View/Model if needed
                                    //  TODO:   See if need to call model CloseRequest method
                                    region.Remove(element);
                                    element.StateChanged -= handler;
                                }
                            };
                            element.StateChanged += handler;
                        }
                        break;
                    //case NotifyCollectionChangedAction.Remove:
                    //    foreach (DockableContent element in e.OldItems)
                    //    {
                    //        if (regionTarget.Items.Contains(element))
                    //        {
                    //            regionTarget.Items.Remove(element);
                    //        }
                    //        //element.StateChanged -= element_StateChanged;
                    //    }
                    //    break;
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
}
